/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.service;

import cz.muni.dao.ShipmentDaoLocal;
import cz.muni.dto.ShipmentDTO;
import cz.muni.entities.Courier;
import cz.muni.entities.Shipment;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;

/**
 *
 * @author sparrow
 */
@Stateless
@Local(value=ShipmentServiceLocal.class)
public class ShipmentService implements ShipmentServiceLocal{
    @Resource
    private SessionContext ctx;
    
    @EJB
    private ShipmentDaoLocal shipmentDao;

    private DtoConversionHelper conversion = new DtoConversionHelper();
    
    @Override
    public void create(ShipmentDTO s) {
        Shipment ship = mapDTOtoShipment(s);
         shipmentDao.create(ship);
        s.setId(ship.getId());
    }

    @Override
    public void delete(ShipmentDTO t) {
        shipmentDao.delete(mapDTOtoShipment(t));
    }

    @Override
    public ShipmentDTO find(Long id) {
        return mapShipmentToDTO(shipmentDao.find(id));
    }

    @Override
    public ShipmentDTO update(ShipmentDTO t) {
        return mapShipmentToDTO(shipmentDao.update(mapDTOtoShipment(t)));
    }

    @Override
    public List<ShipmentDTO> findAll() {
        List<ShipmentDTO> dtoList = new ArrayList<ShipmentDTO>();
        for (Shipment s : shipmentDao.findAll()) {
            dtoList.add(mapShipmentToDTO(s));
        }
        return dtoList;
    }

    @Override
    public List<ShipmentDTO> findByState(String state) {
        List<ShipmentDTO> dtoList = new ArrayList<ShipmentDTO>();
        for (Shipment s : shipmentDao.findByState(state)) {
            dtoList.add(mapShipmentToDTO(s));
        }
        return dtoList;
    }

    @Override
    public List<ShipmentDTO> findByStateAndCourier(String state, Courier courier) {
        List<ShipmentDTO> dtoList = new ArrayList<ShipmentDTO>();
        for (Shipment s : shipmentDao.findByStateAndCourier(state, courier)) {
            dtoList.add(mapShipmentToDTO(s));
        }
        return dtoList;
    }

    @Override
    public ShipmentDTO findByTrackingNumber(Long number) {
        return mapShipmentToDTO(shipmentDao.findByTrackingNumber(number));
    }
    
    private Shipment mapDTOtoShipment(ShipmentDTO t) {
        if (t == null) {
            return null;
        }
        Shipment s = new Shipment();
        s.setId(t.getId());
        s.setCourier(conversion.mapFromDTO(t.getCourier()));
        s.setCreationDate(t.getCreationDate());
        s.setDescription(t.getDescription());
        s.setDistance(t.getDistance());
        s.setPrice(t.getPrice());
        s.setRecipient(conversion.mapFromDTO(t.getRecipient()));
        s.setSender(conversion.mapFromDTO(t.getSender()));
        s.setTarrif(conversion.mapFromDTO(t.getTarrif()));
        s.setShipmentState(t.getShipmentState());
        s.setTrackingNumber(t.getTrackingNumber());;
        s.setWeight(t.getWeight());
        return s;
    }
    
        
    private ShipmentDTO mapShipmentToDTO(Shipment t) {
        if (t == null) {
            return null;
        }
        ShipmentDTO s = new ShipmentDTO();
        s.setId(t.getId());
        s.setCourier(conversion.mapToDTO(t.getCourier()));
        s.setCreationDate(t.getCreationDate());
        s.setDescription(t.getDescription());
        s.setDistance(t.getDistance());
        s.setPrice(t.getPrice());
        s.setRecipient(conversion.mapToDTO(t.getRecipient()));
        s.setSender(conversion.mapToDTO(t.getSender()));
        s.setTarrif(conversion.mapToDTO(t.getTarrif()));
        s.setShipmentState(t.getShipmentState());
        s.setTrackingNumber(t.getTrackingNumber());
        s.setWeight(t.getWeight());
        return s;
    }
}
